﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using Utility;
using System.Text;
using System.Security.Cryptography;


namespace WEBSITE_BANDEAL.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // Response.Write(DateTime.Now.GetDateTimeFormats();
        }
        public static string md5(string source)
        {
            using (MD5 md5Hash = MD5.Create())
                return GetMd5Hash(md5Hash, source);
        }
        private static string GetMd5Hash(MD5 md5Hash, string input)
        {
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
                sBuilder.Append(data[i].ToString("x2"));
            return sBuilder.ToString();
        }
        protected void btn_Login_Click(object sender, EventArgs e)
        {
            Criteria cri = new Criteria();
            cri.add(Condition.AND, "USER_NAME", Condition.EQUAL, username.Text);
            int total = 0;
            AdminColection admin = ADMINBUS.GET("", 0, 0, "MA_AD", OrderDirection.ASC.ToString(), total);
            if (admin.Count > 0)
            {
                if (admin[0].Pass == md5(password.Text))
                {
                    Session["UserName"] = admin[0].HoTen;
                    Session["Ma"] = admin[0].Ma_ad;
                    Response.Redirect("~/Admin/Default.aspx");
                }
                else
                {
                    lit_error.Text = "Mật khẩu sai. Vui lòng nhập lại";
                }
            }
            else
            {
                lit_error.Text = "Tài khoản không tồn tại. Vui lòng kiểm tra lại";
            }

        }

       
    }
}